package com.ficus.road.maintain.third.zh;

import com.alibaba.fastjson.JSONArray;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.zh.IllegalDiggingZH;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.strategy.allfactory.AllFactoryContext;
import com.ficus.road.maintain.strategy.allfactory.IllegalDiggingStrategy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2022/4/6 上午11:02
 */
@Component
public class ZHIllegalDigging extends BaseZH {

    @Autowired
    private HitAllFactoryDao hitAllFactoryDao;

    @PostConstruct
    void init() {
        arg0 = "sanwangRoadopenningpatrol";
        arg1 = "{\"handlestate\":\"1\"}";
        errMsg = "调用中海接口获取非法掘路异常!";
        successMsg = "调用中海接口获取非法掘路成功!";
    }

    //    @Scheduled(cron = "*/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "0 0 12,14,16 * * ?", zone = "GMT+8:00")
    public void refreshIllegalDigging() {
        String illegalDiggingStr = getZHData();
        if (StringUtils.isEmpty(illegalDiggingStr)) {
            return;
        }
        AllFactoryContext allFactoryContext = new AllFactoryContext(new IllegalDiggingStrategy(hitAllFactoryDao));
        List<IllegalDiggingZH> illegalDiggingZHList = JSONArray.parseArray(illegalDiggingStr, IllegalDiggingZH.class);
        illegalDiggingZHList.forEach(zh -> {
            Hit hit = allFactoryContext.convert2AllFactoryHit(zh);
            hitAllFactoryDao.saveOrUpdate(hit);
        });
    }
}
